//
//  TranRecordModifyView.swift
//  ptbuy
// 交易记录修改页面
//  Created by MyMac on 2024/12/5.
//

import SwiftUI

struct TranRecordModifyView: View {
    
    @Binding var tranRecordCard:Bool
    @Environment(\.managedObjectContext) private var viewContext
    
    @ObservedObject var modigyClient:Client
    @Binding var isModifyClientFailAlert:Bool
    
    //修改的客户名称
  @State private var modifyName:String = ""
  //修改的客户电话
  @State private var modifyPhone:String = ""
  //修改的收购人
  @State private var modifyAgentName:String = ""
    
    var body: some View {
        NavigationView {
            VStack{
                VStack{
                Form {
                    Section(header: Text("")) {
                        HStack{
                            Text("客户名称：").font(.system(size: 15))
                            TextField("客户名称",text: $modifyName).minimumScaleFactor(0.3)
                                .textFieldStyle(
                                    RoundedBorderTextFieldStyle()
                                ).font(.system(size: 15))
                           
                        }.padding([.top,.bottom],8)
                        
                        HStack{
                            Text("客户电话：").font(.system(size: 15))
                            TextField("客户电话",text: $modifyPhone).keyboardType(.numberPad)
                                .textFieldStyle(
                                    RoundedBorderTextFieldStyle()
                                ).font(.system(size: 15))
                               
                        }.padding([.top,.bottom],8)
                        HStack{
                            Text("收  购  人：").font(.system(size: 15))
                            TextField("收  购  人",text: $modifyAgentName)
                                .textFieldStyle(
                                    RoundedBorderTextFieldStyle()
                                ).font(.system(size: 15))
                               
                        }.padding([.top,.bottom],8)
                        
                    }
                    
                }
            }.frame(maxWidth: .infinity)
                .background(Color.white)
                .cornerRadius(8)
                .shadow(radius: 8)
                            
            Spacer()
            }.onAppear{
                if let  myName = modigyClient.name{
                    modifyName = myName
                }
                if let  myPhone = modigyClient.phone{
                    modifyPhone = myPhone
                }
                if let  myAgentName = modigyClient.agent_name{
                    modifyAgentName = myAgentName
                }
                
            }.frame(maxWidth:.infinity,maxHeight: .infinity).background(
                // 创建渐变背景
                LinearGradient(
                    gradient: Gradient(
                        colors: [
                            Color("startColor").opacity(
                                10
                            ),
                            // 上半部分颜色，增加不透明度
                            Color("endColor").opacity(0.1)
                        ]// 上半部分颜色，增加不透明度
                    ),
                    startPoint: .top,
                    endPoint: .bottom
                )
                .edgesIgnoringSafeArea(.all)  // 确保背景覆盖整个屏幕
            ).navigationBarTitle("信息修改", displayMode: .inline).navigationViewStyle(
                StackNavigationViewStyle()
            ) // 推荐在全屏时使用此样式
            .background()
            .toolbar {
                // 修改标题字体大小
                ToolbarItem(placement: .topBarLeading) {
                    Text("返回")
                        .clipShape(
                            Capsule()
                        ) // 使用 Capsule 形状来实现圆角效果
                        .onTapGesture {
                            tranRecordCard = false
                        }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Text("保存")
                        .clipShape(
                            Capsule()
                        ) // 使用 Capsule 形状来实现圆角效果
                        .onTapGesture {
                            modifyClient()
                        }
                }
            }
        }
    }
    
    //修改客户信息
       func modifyClient(){
           tranRecordCard = false
           modigyClient.agent_name = modifyAgentName
           modigyClient.name = modifyName
           modigyClient.phone = modifyPhone
           do{
               try viewContext.save()
           }catch{
               isModifyClientFailAlert = true
           }
       }
}
